#ifndef _LIB_LICENSE_H_INCLUDE_
#define _LIB_LICENSE_H_INCLUDE_

#include <wx/wx.h>
#include <wx/stdpaths.h>
#include <wx/filename.h>
#include <wx/datetime.h>
#include <wx/buffer.h>
#include <stdlib.h>
#include <functional> // for std::hash
#include <sstream>
#include <iomanip>
#include <map>

#ifdef _WIN32
#include <windows.h>
#include <iphlpapi.h>
#pragma comment(lib, "iphlpapi.lib")
#elif defined(__linux__)
#include <fstream>
#elif defined(__APPLE__)
#include <sys/sysctl.h>
#endif

struct tagLicenseHearder
{
    tagLicenseHearder()
        : dataSize(0)
    {
        memcpy(mark,"AUT",3);
    }

    char mark[3];      // 'A' 'U' 'T'
    int dataSize;      // 数据长度
};

struct tagLicenseItem
{
    tagLicenseItem()
        : createtime(0),authtime(0)
    {
        memset(name,0,128);
        memset(muid,0,128);
        memset(authcode,0,128);
        memset(comment,0,256);
    }
    tagLicenseItem(wxString pname,wxString pmuid,
                   wxString pauthcode,
                   int64_t pcreatetime,int64_t pauthtime,
                   wxString pcomment)
    {
        createtime = pcreatetime;
        authtime = pauthtime;
        memset(name,0,128);
        memset(muid,0,128);
        memset(authcode,0,128);
        memset(comment,0,256);

        if(!pname.IsEmpty())
        {
            std::string decStr = pname.ToStdString();
            memcpy(name,decStr.c_str(),decStr.length());
            name[decStr.length()] = '\0';
        }
        if(!pmuid.IsEmpty())
        {
            std::string decStr = pmuid.ToStdString();
            memcpy(muid,decStr.c_str(),decStr.length());
            muid[decStr.length()] = '\0';
        }
        if(!pauthcode.IsEmpty())
        {
            std::string decStr = pauthcode.ToStdString();
            memcpy(authcode,decStr.c_str(),decStr.length());
            authcode[decStr.length()] = '\0';
        }
        if(!pcomment.IsEmpty())
        {
            std::string decStr = pcomment.ToStdString();
            memcpy(comment,decStr.c_str(),decStr.length());
            comment[decStr.length()] = '\0';
        }
    }

    char name[128];
    char muid[128];
    char authcode[128];
    int64_t createtime;
    int64_t authtime;
    char comment[256];
};

class CLicense
{
public:
    CLicense();
    ~CLicense();

    /// 生成授权文件
    bool generateAuthorizationFile(void);
    /// 导入指定文件的授权文件到系统中
    const tagLicenseItem* loadAuthorizationFromFile(wxString authfilepath);
    /// 开始授权并保存到指定的文件
    const tagLicenseItem* startAuthorizationAndSaveFile(wxString muid,wxString authfilepath);
    /// 添加一个授权到系统中
    bool addAuthorization(tagLicenseItem pLicenseItem);
    /// 更新授权
    bool updateAuthorization(tagLicenseItem pLicenseItem);
    /// 删除授权
    void deleteAuthorizations(wxArrayString autArray);
    /// 删除授权
    void deleteAuthorization(wxString Muid);
    /// 检测系统是否授权
    bool isAuthorization(void);
    /// 清除所有的授权
    inline void clearAllAuthorizations(void) { m_LicenseItems.clear(); }
    /// 得到指定muid的授权
    const tagLicenseItem* getAuthorization(wxString autName);
    /// 得到所有的授权项
    const std::map<wxString,tagLicenseItem>& getAllAuthItems(void) { return m_LicenseItems; }
    /// 随机得到提示语
    const wxString getTipString(void);

private:
    /// 得到机器唯一ID
    wxString GetMachineUniqueId();

private:
    std::map<wxString,tagLicenseItem> m_LicenseItems;
    wxArrayString m_tipStrings;
};

#endif // _LIB_LICENSE_H_INCLUDE_
